﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MethodWorx.Core.DomainModel;

namespace MethodWorx.CMS.Content
{
    [Serializable]
    public class ContentTemplateConstraintCollection : ChildEntityCollection<PageTemplate, ContentTemplateConstraint>
    {
        public ContentTemplateConstraintCollection(PageTemplate parent,
            Func<PageTemplate, IEnumerable<ContentTemplateConstraint>> loader) : base(parent, loader)
        {

        }

        public void Add(ContentTemplate contentTemplate, int? minimumRequired, int? maximumAllowed, string nameConstraint)
        {
            if (contentTemplate == null)
                throw new ArgumentNullException("contentTemplate");

            if (minimumRequired.HasValue && minimumRequired <= 0)
                throw new ArgumentException("MinimumRequired has to be greater than 0 if speficied");

            if (maximumAllowed.HasValue && maximumAllowed <= 0)
                throw new ArgumentException("MaximumAllowed has to be greater than 0 if specified");

            if (this.FirstOrDefault(pt => pt.RelatedTemplate.Equals(contentTemplate)) != null)
                throw new ArgumentException("Constraint already exists");

            base.Add(
                new ContentTemplateConstraint()
                {
                    RelatedTemplate = contentTemplate,
                    MaximumAllowed = maximumAllowed,
                    MinimumRequired = minimumRequired,
                    NameConstraint = nameConstraint
                });
        }

        public void Remove(ContentTemplateConstraint constraint)
        {
            base.Remove(constraint);
        }
    }
}
